package com.hopu.v1.service.admin.exam.impl;

import com.aliyun.oss.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hopu.v1.exception.BusinessException;
import com.hopu.v1.mapper.admin.exam.ExamOptionAnswerMapper;
import com.hopu.v1.pojo.exam.ExamOptionAnswer;
import com.hopu.v1.pojo.exam.ExamQuestions;
import com.hopu.v1.service.admin.exam.ExamOptionAnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author lihao
* @description 针对表【t_exam_option_answer(考试选项答案表)】的数据库操作Service实现
* @createDate 2024-09-25 11:24:44
*/
@Service
public class ExamOptionAnswerServiceImpl extends ServiceImpl<ExamOptionAnswerMapper, ExamOptionAnswer>
    implements ExamOptionAnswerService {

    @Autowired
    private ExamOptionAnswerMapper examOptionAnswerMapper;

    /**
     * 新增
     * @param examOptionAnswer
     */
    @Override
    public void add(ExamOptionAnswer examOptionAnswer) {
        examOptionAnswer.setCreateTime(LocalDateTime.now());
        save(examOptionAnswer);
    }

    /**
     * 修改
     * @param examOptionAnswer
     */
    @Override
    public void update(ExamOptionAnswer examOptionAnswer) {
        if(examOptionAnswerMapper.selectById(examOptionAnswer.getId()) != null) {
            examOptionAnswer.setUpdateTime(LocalDateTime.now());
            updateById(examOptionAnswer);
        }else{
            throw new BusinessException("选项不存在");
        }
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void del(Integer id) {
        if(examOptionAnswerMapper.selectById(id) == null) {
            throw new BusinessException("选项不存在");
        }else{
            examOptionAnswerMapper.deleteById(id);
        }
    }

    /**
     * 查询列表
     * @param page
     * @param examOptionAnswer
     * @param endTime
     * @return
     */
    @Override
    public IPage<ExamOptionAnswer> selectList(Page<ExamOptionAnswer> page, ExamOptionAnswer examOptionAnswer, String endTime) {
        LambdaQueryWrapper<ExamOptionAnswer> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isNullOrEmpty(examOptionAnswer.getOptionText())) {
            wrapper.like(ExamOptionAnswer::getOptionText, examOptionAnswer.getOptionText());
        } else if (examOptionAnswer.getCreateTime() != null) {
            wrapper.between(ExamOptionAnswer::getCreateTime, examOptionAnswer.getCreateTime(),endTime);
        }
        wrapper.orderByDesc(ExamOptionAnswer::getCreateTime);
        return examOptionAnswerMapper.selectPage(page, wrapper);
    }

    /**
     * 批量插入
     * @param allOptions
     */
    @Override
    public void batchAdd(List<ExamOptionAnswer> allOptions) {
        examOptionAnswerMapper.insert(allOptions);
    }
}




